/*
* Synchronized: 锁关键字
* 使用时需要搭配一个代码块{} 进入就会上锁,出去就会解锁
* 在已经加锁的状态中,另一个线程尝试去加这个锁,就会产生锁竞争/锁冲突,后一个线程就会阻塞等待
* */

import com.sun.source.tree.SynchronizedTree;

public class Demo10 {
    public static int count;

    public static void main(String[] args) throws InterruptedException {

        Object locker = new Object();

        Thread t1 = new Thread(()->{
            for (int i = 0; i < 50000; i++) {
                synchronized (locker){
                    count++;
                }
            }
        });

        Thread t2 = new Thread(()->{
            for (int i = 0; i < 50000; i++) {
                synchronized(locker) {
                    count++;
                }
            }
        });

        t1.start();

        t2.start();

        t1.join();

        t2.join();

        System.out.println(count);
    }
}
